package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IAddPutForwardActivityModel;
import com.wgts.wgts.mvp.model.ISendSMSCodeModel;
import com.wgts.wgts.mvp.presenter.IAddPutForwardActivityPresenter;
import com.wgts.wgts.mvp.view.IAddPutForwardActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.AddPutForwardReq;
import com.wgts.wgts.netmodle.req.SmsCodeRequest;
import com.wgts.wgts.netmodle.resp.CurrentEntity;
import com.wgts.wgts.netmodle.resp.PutForwardTypeEntity;
import com.wgts.wgts.rxjava.MVPResultSubscriber;

public class AddPutForwardActivityPresenter implements IAddPutForwardActivityPresenter {

    private IAddPutForwardActivityModel model;

    private IAddPutForwardActivityView view;

    private ISendSMSCodeModel codeModel;


    public AddPutForwardActivityPresenter(IAddPutForwardActivityModel model, IAddPutForwardActivityView view, ISendSMSCodeModel codeModel) {
        this.model = model;
        this.view = view;
        this.codeModel = codeModel;
    }

    @Override
    public void addPutForward(String bankName, String cardCode, String verifyCode, String withdrawType,  String userName, String bankAccount) {
        AddPutForwardReq req = new AddPutForwardReq();
        req.bankName = bankName;
        req.cardCode = cardCode;
        req.verifyCode = verifyCode;
        req.withdrawType  = withdrawType;
        req.userName = userName;
        req.bankAccount = bankAccount;
        model.addPutForward(req, new HttpResultSubscriber<PutForwardTypeEntity>() {
            @Override
            public void onSuccess(PutForwardTypeEntity putForwardTypeEntity) {
                view.putForwardResult();
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void sendSms(String phone, String scene) {
        SmsCodeRequest smsCodeRequest = new SmsCodeRequest();
        smsCodeRequest.scene = scene;
        smsCodeRequest.phone = phone;
        codeModel.sendSMS(smsCodeRequest, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                view.sendSmsResult();
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(0x111, msg);
            }
        });
    }

    @Override
    public void addCountDownTimeListener() {
        codeModel.setCutDownTime(new MVPResultSubscriber<Integer>() {
            @Override
            public void netx(Integer integer) {
                if(integer == 0){
                    view.countDownTimeOver();
                    removeCountDownTimeListener();
                    return;
                }
                view.countDowmTime(integer);
            }
        });
    }

    @Override
    public void removeCountDownTimeListener() {
        codeModel.removeCutDownTime();
    }
}
